package cn.glenn.weibo0521.controller;

import cn.glenn.weibo0521.base.respone.JsonResult;
import cn.glenn.weibo0521.mapper.CommentMapper;
import cn.glenn.weibo0521.pojo.dto.CommentAddParam;
import cn.glenn.weibo0521.pojo.enity.Comment;
import cn.glenn.weibo0521.pojo.vo.CommentVO;
import cn.glenn.weibo0521.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/comment/")
@Tag(name = "03-评论模块",description = "03-评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @Parameters({
            @Parameter(name = "content",description = "评论内容"),
            @Parameter(name = "weiboId",description = "微博ID")
    })
    @Operation(summary = "评论微博")
    @ApiOperationSupport(order = 1)
    public JsonResult insertComment(CommentAddParam commentAddParam, HttpSession session) {
        Comment comment = new Comment();
        UserVO user = (UserVO) session.getAttribute("user");
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setUserId(user.getId());
        comment.setCreateTime(new Date());
        int result = commentMapper.insertComment(comment);
        log.debug("result:{}", result);
        return JsonResult.success();
    }

    @GetMapping("selectByWeiboId")
    @Parameter(name = "id",description = "微博ID")
    @Operation(summary = "评论列表")
    @ApiOperationSupport(order = 2)
    public JsonResult commentList(Long id) {
        List<CommentVO> commentList = commentMapper.commentList(id);
        return JsonResult.success(commentList);
    }




}
